package graduate.cslg.com.universityshopping.card;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.entity.CardBean;
import graduate.cslg.com.universityshopping.net.CardResponse;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LostFoundActivity extends AppCompatActivity {

    private Button submitButton;
    private EditText cardPass;
    private String originPass, getUserName;
    private String getUserAccount;
    private ProgressDialog progressDialog;
    private CardBean mCardBean;
    private TextView userName;
    private TextView userAccount;
    private TextView cardStatus;
    private LoginResponse mLoginResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lost_found);

        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315222");
        getUserName = preferences.getString("user_name", "Tom wang");
        initViews();
        userName.setText(getUserName);
        userAccount.setText(getUserAccount);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        getCardInfo();

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cardPass.getText().toString().equals(originPass)) {
                    showProgressDialog();
                    //更新卡状态为挂失状态
                    updateCardStatus();
                } else {
                    Toast.makeText(LostFoundActivity.this, "密码错误！", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void updateCardStatus() {
        RetrofitDevelop.getTestService()
                .lostCard(getUserAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Toast.makeText(LostFoundActivity.this, "挂失成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(LostFoundActivity.this, "挂失失败，请稍后再试！", Toast.LENGTH_SHORT).show();
                        }
                        closeProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(LostFoundActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                        closeProgressDialog();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }

    private void initViews() {
        submitButton = findViewById(R.id.submit_button);
        cardPass = findViewById(R.id.user_card_pass);
        userName = findViewById(R.id.user_name);
        userAccount = findViewById(R.id.user_card_id);
        cardStatus = findViewById(R.id.user_card_status);
    }

    private void getCardInfo() {
        RetrofitDevelop.getTestService()
                .getCardInfo(getUserAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CardBean>() {
                    @Override
                    public void onCompleted() {
                        int result = mCardBean.getCard_type();
                        originPass = mCardBean.getCard_password();
                        if (result == 1) {
                            cardStatus.setText("正常");
                            submitButton.setVisibility(View.VISIBLE);
                            cardPass.setEnabled(true);
                        } else {
                            submitButton.setVisibility(View.INVISIBLE);
                            cardPass.setEnabled(false);
                            cardStatus.setText("挂失中...");
                            cardPass.setText("您的卡已挂失");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(CardBean cardBean) {
                        mCardBean = cardBean;
                    }
                });
    }

    /**
     * 显示进度对话框
     */
    private void showProgressDialog(){
        if(progressDialog==null){
            progressDialog=new ProgressDialog(this);
            progressDialog.setMessage("信息登记中，请稍等");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }
    /**
     * 关闭进度对话框
     */
    private void closeProgressDialog(){
        if(progressDialog!=null){
            progressDialog.dismiss();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
